package com.leetcode.partition2;

import com.leetcode.common.TreeNode;

/**
 * @author `RKC`
 * @date 2021/8/22 9:05
 */
public class LC108将有序数组转换为二叉搜索树 {

    public static TreeNode sortedArrayToBST(int[] nums) {
        return divide(nums, 0, nums.length - 1);
    }

    public static void main(String[] args) {
        int[] nums = { -10, -3, 0, 5, 9 };
        TreeNode root = sortedArrayToBST(nums);
        System.out.println(root.val);
    }

    private static TreeNode divide(int[] nums, int start, int end) {
        if (start > end) return null;
        int mid = start + (end - start) / 2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = divide(nums, start, mid - 1);
        node.right = divide(nums, mid + 1, end);
        return node;
     }
}
